#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int p[N];
int n, m;
struct S
{
	int a, b, c;
}s[N];
int find(int x)
{
	if (x != p[x]) {
		p[x] = find(p[x]);
	}
	return p[x];
}
int cmp(S a, S b)
{
	return a.c < b.c;
}
int kruskal()
{
	sort(s, s + m, cmp);
	LL ans = 0;
	for (int i = 1; i <= n; i++) {
		p[i] = i;
	}
	for (int i = 0; i < m; i++) {
		int a = s[i].a;
		int b = s[i].b;
		int w = s[i].c;
		a = find(a);
		b = find(b);
		if (a != b) {
			p[a] = b;
			ans += w;
		}
	}
	return ans;
}
int main()
{
	cin >> n >> m;
	int i = 0;
	int z = m;
	while (z--) {
		int a, b, c;
		scanf_s("%d%d%d", &a, &b, &c);
		s[i++] = { a,b,c };
	}
	cout << kruskal() << endl;
	return 0;
}